//
//  AllKeyView.swift
//  TLCLockDemo
//
//  Created by run on 2024/7/24.
//

import SwiftUI
import TLCLock

struct AllKeyView: View {


    let device: LockDevice
    @State var keyModel: KeyModel? = nil

    var body: some View {
        
        VStack {
            YxrActionView(
                data: [
                    (0, "管理员密码"),
                    (1, "普通密码"),
                    (2, "门卡钥匙"),
                    (3, "所有"),
                    (4, "指纹钥匙"),
                ], column: 3,
                itemBuilder: { value in
                    YXRActionItem(text: value.1) {
                        switch value.0 {
                        case 0: uploadKeys(type: .adminPwd)
                        case 1: uploadKeys(type: .pwd)
                        case 2: uploadKeys(type: .card)
                        case 3: uploadKeys(type: .all)
                        case 4: uploadKeys(type: .fingerprint)
                        default:
                            print("未实现的方法")
                        }
                    }
                }
            )
            .padding(.horizontal, 10)
            
            // LogView()
            List {
                if let m = keyModel {
                    Text("钥匙总数：\(m.totalCount)")
                        .font(.system(size: 12))
                    
                    if let pwdList: [TLCLock.PasswordModel] = m.passwordList, !pwdList.isEmpty {
                        Text("密码：").font(.system(size: 12.0))
                        
                        ForEach(pwdList) { item in
                            
                            VStack(alignment: .leading, spacing: 3.0) {
                                Text("密码：\(item.password)").font(.system(size: 12.0))
                                Text("密码索引：\(String(item.pwdId))").font(.system(size: 12.0))
                                Text("状态：\(item.state.description)").font(.system(size: 12.0))
                                if let begin = item.from {
                                    Text("开始时间：\(begin, formatter: yMdHmsFormatter)").font(.system(size: 12.0))
                                }
                                if let end = item.to {
                                    Text("结束时间：\(end, formatter: yMdHmsFormatter)").font(.system(size: 12.0))
                                } else {
                                    Text("有效期: 永久").font(.system(size: 12.0))
                                }
                                if item.pwdId == 1 {
                                    Text("管理员密码").font(.system(size: 12.0))
                                }
                            }
                        }
                    }
                    
                    if let cardList = m.cardList, !cardList.isEmpty {
                        Text("门卡：").font(.system(size: 12.0))
                        ForEach(cardList) { item in
                            VStack(alignment: .leading, spacing: 3.0) {
                                Text("卡号：\(item.cardNumber)").font(.system(size: 12.0))
                                Text("卡索引：\(String(item.cardId))").font(.system(size: 12.0))
                                Text("类型：\(item.type.description)").font(.system(size: 12.0))
                                Text("状态：\(item.state.description)").font(.system(size: 12.0))
                                if let begin = item.from {
                                    Text("开始时间：\(begin, formatter: yMdHmsFormatter)").font(.system(size: 12.0))
                                }
                                if let end = item.to {
                                    Text("结束时间：\(end, formatter: yMdHmsFormatter)").font(.system(size: 12.0))
                                } else {
                                    Text("有效期: 永久").font(.system(size: 12.0))
                                }
                            }
                        }
                    }
                    
                    if let fpList = m.fingerprintList, !fpList.isEmpty {
                        Text("指纹：").font(.system(size: 12.0))
                        ForEach(fpList) { item in
                            VStack(alignment: .leading, spacing: 3.0) {
                                Text("指纹索引：\(String(item.fpId))").font(.system(size: 12.0))
                                Text("状态：\(item.state.description)").font(.system(size: 12.0))
                                if let begin = item.from {
                                    Text("开始时间：\(begin, formatter: yMdHmsFormatter)").font(.system(size: 12.0))
                                }
                                if let end = item.to {
                                    Text("结束时间：\(end, formatter: yMdHmsFormatter)").font(.system(size: 12.0))
                                } else {
                                    Text("有效期: 永久").font(.system(size: 12.0))
                                }
                            }
                        }
                    }
                } else {
                    Text("点击上面的按钮，开始获取钥匙")
                        .font(.system(size: 12))
                }
            }
        }
        
        .yxrNavigationTitle("获取门锁钥匙")
    }

    func uploadKeys(type: OpenLockType) {
        guard let lockKey = Server.getSecret(device.macAddress) else {
            showToast("没有lockKey")
            return
        }

        Task {
            showLoading()
            let result = await device.getAllOpenLockKey(type, lockKey: lockKey, trackId: Server.testTrackId)
            hideLoading()
            if result.isFailure {
                keyModel = nil
                showToast(result.errorMessage)
                return
            }
            let value = result.value!
            Log.info("total: \(value.totalCount)")
            Log.info("pwd: \(value.passwordList ?? [])")
            Log.info("card: \(value.cardList ?? [])")
            Log.info("fingerprint: \(value.fingerprintList ?? [])")
            keyModel = value
            
            showToast("获取钥匙成功")
        }
    }
}
